package at.fhj.itm10.ticketmaster.helper;
/**
 * 
 * @author Gerald Hambrosch
 *
 */
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import at.fhj.itm10.ticketmaster.entities.Address;
import at.fhj.itm10.ticketmaster.services.EventService;
import at.fhj.itm10.ticketmaster.services.ServiceFactory;

public class AddressConverter implements Converter {

	

	ServiceFactory factory = new ServiceFactory();
	Address a;

	public Object getAsObject(FacesContext context, UIComponent component, String value)
			throws ConverterException {
		
		if (value == null || value.equals("")) {
            return "";
        } 
		else 
		{
					EventService service = new ServiceFactory().getEventService();
					long id = Long.parseLong((value));  	

								a = factory.getAddressService().findById(id);
								service.doExtendedSearch(a, null, null, null);
							
                    return a;
        }

	}

	public String getAsString(FacesContext context, UIComponent component, Object value)
			throws ConverterException {
	       if (value == null || value.equals("")) {
	            return "";
	        } else {
	            return String.valueOf(((Address) value).getId());
	        }

	}

}
